package com.agung.wonogirimaps;

import java.io.File;
import java.util.List;

import org.json.JSONObject;

import com.agung.wonogirimaps.adapter.CustomAdapter;
import com.agung.wonogirimaps.entity.Lokasi;
import com.agung.wonogirimaps.util.JSONAdapter;
import com.agung.wonogirimaps.util.Utils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;

public class SearchResultActivity extends ActionBarActivity
{
	private JSONAdapter					json;
	private ProgressDialog				pDialog;

	private CustomAdapter				adapter;

	private ListView					lv;

	private ImageLoader					imageLoader;
	private ImageLoaderConfiguration	config;
	private DisplayImageOptions			options;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_result);

		lv = (ListView) findViewById(R.id.list_search);
		lv.setEmptyView(findViewById(R.id.notfound));
		json = new JSONAdapter();

		File cacheDir = StorageUtils.getCacheDirectory(this);

		imageLoader = ImageLoader.getInstance();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_launcher).cacheOnDisc(true)
				.delayBeforeLoading(100).cacheInMemory(true)
				.displayer(new RoundedBitmapDisplayer(20)).build();

		config = new ImageLoaderConfiguration.Builder(this)
				.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
				.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
				.writeDebugLogs().build();

		imageLoader.init(config);

		getSupportActionBar().setTitle("Hasil Pencarian");
		handleIntent(getIntent());
	}

	@Override
	protected void onNewIntent(Intent intent)
	{
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		handleIntent(intent);
	}

	private void handleIntent(Intent intent)
	{
		if (Intent.ACTION_SEARCH.equals(intent.getAction()))
		{
			String query = intent.getStringExtra(SearchManager.QUERY);
			Toast.makeText(this, "Cari : " + query, Toast.LENGTH_SHORT).show();

			Utils.TRACE(SearchResultActivity.class.getName(), Utils.API_SEARCH
					+ query);
			// new AsyncSearch().execute(Utils.API_SEARCH + query);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search_result, menu);
		return true;
	}

	@Override
	public void onBackPressed()
	{
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

	// private class AsyncSearch extends AsyncTask<String, Void, List<Lokasi>>
	// {
	// @Override
	// protected List<Lokasi> doInBackground(String... params)
	// {
	// JSONObject obj = json.getJSONFromURL(params[0]);
	// //return json.getLocationBySearch(obj);
	// return json.getLocationBySearch(obj);
	// }
	//
	// @Override
	// protected void onPostExecute(List<Lokasi> result)
	// {
	// // TODO Auto-generated method stub
	// super.onPostExecute(result);
	// pDialog.dismiss();
	//
	// adapter = new CustomAdapter(SearchResultActivity.this, imageLoader,
	// options, result);
	// lv.setAdapter(adapter);
	// }
	//
	// @Override
	// protected void onPreExecute()
	// {
	// // TODO Auto-generated method stub
	// super.onPreExecute();
	// pDialog = new ProgressDialog(SearchResultActivity.this);
	// pDialog.setMessage("Loading");
	// pDialog.setCancelable(true);
	// pDialog.show();
	// }
	// }

}
